package com.fox.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtils {


    //由于这是工具类，就直接将构造方法设置为私有的
    private BeanCopyUtils() {

    }

    public static <T> T copyBean(Object source,Class<T> clazz) {
        //创建目标对象
        T result = null;
        try {
            result = clazz.newInstance();
            //实现属性copy
            BeanUtils.copyProperties(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回结果
        return result;
    }
    public static <E,T> List<T> copyBeanList(List<E> list, Class<T> clazz){
        List<T> result = new ArrayList<>();
        for (E e : list) {
            T t = copyBean(e, clazz);
            result.add(t);
        }
        return result;
//        return list.stream()
//                .map(E -> copyBean(E, clazz))
//                .collect(Collectors.toList());
    }
}
